home *** CD-ROM | disk | FTP | other *** search
/ PC Open 100 / PC Open 100 CD 1.bin / CD1 / INTERNET / EMAIL / pop file / setup.exe / Classifier / popfile.sql < prev    next >
Encoding:
Text File  |  2004-03-16  |  17.6 KB  |  370 lines

  1. -- ---------------------------------------------------------------------------------------------
  2. --
  3. -- popfile.schema - POPFile's database schema
  4. --
  5. -- Copyright (c) 2001-2003 John Graham-Cumming
  6. --
  7. --   This file is part of POPFile
  8. --
  9. --   POPFile is free software; you can redistribute it and/or modify
  10. --   it under the terms of the GNU General Public License as published by
  11. --   the Free Software Foundation; either version 2 of the License, or
  12. --   (at your option) any later version.
  13. --
  14. --   POPFile is distributed in the hope that it will be useful,
  15. --   but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. --   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. --   GNU General Public License for more details.
  18. --
  19. --   You should have received a copy of the GNU General Public License
  20. --   along with POPFile; if not, write to the Free Software
  21. --   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. --
  23. -- ---------------------------------------------------------------------------------------------
  24.  
  25. -- An ASCII ERD (you might like to find the 'users' table first and work from there)
  26. --
  27. --      +---------------+       +-----------------+
  28. --      | user_template |       | bucket_template |
  29. --      +---------------+       +-----------------+
  30. --      |      id       |---+   |       id        |---+
  31. --      |     name      |   |   |      name       |   |
  32. --      |     def       |   |   |       def       |   |
  33. --      +---------------+   |   +-----------------+   |
  34. --                          |                         |
  35. --      +---------------+   |     +---------------+   |
  36. --      |  user_params  |   |     | bucket_params |   |
  37. --      +---------------+   |     +---------------+   |
  38. --      |      id       |   |     |      id       |   |
  39. --  +---|    userid     |   | +---|   bucketid    |   |
  40. --  |   |     utid      |---+ |   |     btid      |---+
  41. --  |   |     val       |     |   |     val       |
  42. --  |   +---------------+     |   +---------------+
  43. --  |                         |                      +----------+
  44. --  |                         |                      |  matrix  |      +-------+
  45. --  |                         |   +---------+        +----------+      | words |
  46. --  |      +----------+       |   | buckets |        |    id    |      +-------+
  47. --  |      |   users  |       |   +---------+        |  wordid  |------|  id   |
  48. --  |      +----------+    /--+---|    id   |=====---| bucketid |      |  word |
  49. --  +----==|    id    |---(-------| userid  |     \  |  times   |      +-------+
  50. --      /  |   name   |   |       |  name   |     |  | lastseen |
  51. --      |  | password |   |       | pseudo  |     |  +----------+
  52. --      |  +----------+   |       +---------+     |
  53. --      |                 |                       |
  54. --      |                 |        +-----------+  |
  55. --      |                 |        |  magnets  |  |
  56. --      |   +----------+  |        +-----------+  |     +--------------+
  57. --      |   | history  |  |     +--|    id     |  |     | magnet_types |
  58. --      |   +----------+  |     |  | bucketid  |--+     +--------------+
  59. --      |   |   id     |  |     |  |   mtid    |--------|      id      |
  60. --      +---| userid   |  |     |  |   val     |        |     mtype    |
  61. --          |  frm     |  |     |  |   seq     |        |    header    |
  62. --          |   too    |  |     |  +-----------+        +--------------+
  63. --          |   cc     |  |     |
  64. --          | subject  |  |     |
  65. --          | bucketid |--+     |
  66. --          | usedtobe |--/     |
  67. --          | magnetid |--------+
  68. --          |  message |
  69. --          +----------+
  70. --
  71.  
  72. -- TABLE DEFINITIONS
  73.  
  74. -- ---------------------------------------------------------------------------------------------
  75. --
  76. -- users - the table that stores the names and password of POPFile users
  77. --
  78. -- v0.21.0: With this release POPFile does not have an internal concept of
  79. -- 'user' and hence this table consists of a single user called 'admin', once
  80. -- we do the full multi-user release of POPFile this table will be used and
  81. -- there will be suitable APIs and UI to modify it
  82. --
  83. -- ---------------------------------------------------------------------------------------------
  84.  
  85. create table users ( id integer primary key,  -- unique ID for this user
  86.                      name varchar(255),       -- textual name of the user
  87.                      password varchar(255),   -- user's password
  88.                      unique (name)            -- the user name must be unique
  89.                    );
  90.  
  91. -- ---------------------------------------------------------------------------------------------
  92. --
  93. -- buckets - the table that stores the name of POPFile buckets and relates
  94. --           them to users. 
  95. --
  96. -- Note: A single user may have multiple buckets, but a single bucket only has
  97. -- one user.  Hence there is a many-to-one relationship from buckets to users.
  98. --
  99. -- ---------------------------------------------------------------------------------------------
  100.  
  101. create table buckets( id integer primary key, -- unique ID for this bucket
  102.                       userid integer,         -- corresponds to an entry in
  103.                                               -- the users table
  104.                       name varchar(255),      -- the name of the bucket
  105.                       pseudo int,             -- 1 if this is a pseudobucket
  106.                                               -- (i.e. one POPFile uses internally)
  107.                       unique (userid,name)    -- a user can't have two buckets
  108.                                               -- with the same name
  109.                     );
  110.  
  111. -- ---------------------------------------------------------------------------------------------
  112. --
  113. -- words - the table that creates a unique ID for a word.  
  114. --
  115. -- Words and buckets come together in the matrix table to form the corpus of words for
  116. -- each user.
  117. --
  118. -- ---------------------------------------------------------------------------------------------
  119.  
  120. create table words(   id integer primary key, -- unique ID for this word
  121.                       word varchar(255),      -- the word
  122.                       unique (word)           -- each word is unique
  123.                   );
  124.  
  125. -- ---------------------------------------------------------------------------------------------
  126. --
  127. -- matrix - the corpus that consists of buckets filled with words.  Each word
  128. --          in each bucket has a word count.
  129. --
  130. -- ---------------------------------------------------------------------------------------------
  131.  
  132. create table matrix( id integer primary key,   -- unique ID for this entry
  133.                      wordid integer,           -- an ID in the words table
  134.                      bucketid integer,         -- an ID in the buckets table
  135.                      times integer,            -- number of times the word has
  136.                                                -- been seen
  137.                      lastseen date,            -- last time the record was read
  138.                                                -- or written
  139.                      unique (wordid, bucketid) -- each word appears once in a bucket 
  140.                    );
  141.  
  142. -- ---------------------------------------------------------------------------------------------
  143. --
  144. -- user_template - the table of possible parameters that a user can have.  
  145. --
  146. -- For example in the users table there is just an password associated with
  147. -- the user.  This table provides a flexible way of creating per user
  148. -- parameters. It stores the definition of the parameters and the the
  149. -- user_params table relates an actual user with each parameter
  150. --
  151. -- ---------------------------------------------------------------------------------------------
  152.  
  153. create table user_template( id integer primary key,  -- unique ID for this entry
  154.                           name varchar(255),         -- the name of the
  155.                                                      -- parameter
  156.                           def varchar(255),          -- the default value for
  157.                                                      -- the parameter
  158.                           unique (name)              -- parameter name's are unique 
  159.                         );
  160.  
  161. -- ---------------------------------------------------------------------------------------------
  162. --
  163. -- user_params - the table that relates users with user parameters (as defined
  164. --               in user_template) and specific values.
  165. --
  166. -- ---------------------------------------------------------------------------------------------
  167.  
  168. create table user_params( id integer primary key,    -- unique ID for this
  169.                                                      -- entry
  170.                           userid integer,            -- a user
  171.                           utid integer,              -- points to an entry in 
  172.                                                      -- user_template
  173.                           val varchar(255),          -- value for the
  174.                                          -- parameter
  175.                           unique (userid, utid)      -- each user has just one
  176.                                          -- instance of each parameter
  177.                         );
  178.  
  179. -- ---------------------------------------------------------------------------------------------
  180. --
  181. -- bucket_template - the table of possible parameters that a bucket can have.  
  182. --
  183. -- See commentary for user_template for an explanation of the philosophy
  184. --
  185. -- ---------------------------------------------------------------------------------------------
  186.  
  187. create table bucket_template( id integer primary key,  -- unique ID for this entry
  188.                               name varchar(255),       -- the name of the
  189.                                                        -- parameter
  190.                               def varchar(255),        -- the default value for
  191.                                                        -- the parameter
  192.                               unique (name)            -- parameter name's are unique 
  193.                             );
  194.  
  195. -- ---------------------------------------------------------------------------------------------
  196. --
  197. -- bucket_params - the table that relates buckets with bucket parameters (as defined
  198. --                 in bucket_template) and specific values.
  199. --
  200. -- ---------------------------------------------------------------------------------------------
  201.  
  202. create table bucket_params( id integer primary key,    -- unique ID for this
  203.                                                        -- entry
  204.                             bucketid integer,          -- a bucket
  205.                             btid integer,              -- points to an entry in 
  206.                                                        -- bucket_template
  207.                             val varchar(255),          -- value for the
  208.                                            -- parameter
  209.                             unique (bucketid, btid)    -- each bucket has just one
  210.                                            -- instance of each parameter
  211.                         );
  212.  
  213. -- ---------------------------------------------------------------------------------------------
  214. --
  215. -- magnet_types - the types of possible magnet and their associated header
  216. --
  217. -- ---------------------------------------------------------------------------------------------
  218.  
  219. create table magnet_types( id integer primary key,  -- unique ID for this entry
  220.                            mtype varchar(255),      -- the type of magnet
  221.                                                     -- (e.g. from)
  222.                            header varchar(255),     -- the header (e.g. From)
  223.                            unique (mtype)           -- types are unique
  224.                          );
  225.  
  226. -- ---------------------------------------------------------------------------------------------
  227. --
  228. -- magnets - relates specific buckets to specific magnet types with actual
  229. -- magnet values
  230. --
  231. -- ---------------------------------------------------------------------------------------------
  232.  
  233. create table magnets( id integer primary key,    -- unique ID for this entry
  234.                       bucketid integer,          -- a bucket
  235.                       mtid integer,              -- the magnet type
  236.                       val varchar(255),          -- value for the magnet
  237.                       comment varchar(255),      -- user defined comment
  238.                       seq int                    -- used to set the order of magnets
  239.                     );
  240.  
  241. -- MySQL SPECIFIC 
  242.  
  243. -- ---------------------------------------------------------------------------------------------
  244. --
  245. -- NOTE: The following alter table statements are required by MySQL in order
  246. --       to get the ID fields to auto_increment on inserts.
  247. --
  248. -- ---------------------------------------------------------------------------------------------
  249.  
  250. alter table buckets modify id int(11) auto_increment;
  251. alter table bucket_param modify id int(11) auto_increment;
  252. alter table bucket_template modify id int(11) auto_increment;
  253. alter table magnets modify id int(11) auto_increment;
  254. alter table magnet_types modify id int(11) auto_increment;
  255. alter table matrix modify id int(11) auto_increment;
  256. alter table user_params modify id int(11) auto_increment;
  257. alter table user_template modify id int(11) auto_increment;
  258. alter table users modify id int(11) auto_increment;
  259. alter table words modify id int(11) auto_increment;
  260. alter table words modify word binary(255);
  261.  
  262. -- TRIGGERS
  263.  
  264. -- ---------------------------------------------------------------------------------------------
  265. --
  266. -- delete_bucket - if a/some bucket(s) are delete then this trigger ensures
  267. --                 that entries the hang off the bucket table are also deleted
  268. --
  269. -- It deletes the related entries in the 'matrix', 'bucket_params' and
  270. -- 'magnets' tables.  
  271. --
  272. -- ---------------------------------------------------------------------------------------------
  273.  
  274. create trigger delete_bucket delete on buckets
  275.              begin
  276.                  delete from matrix where bucketid = old.id;
  277.                  delete from magnets where bucketid = old.id;
  278.                  delete from bucket_params where bucketid = old.id;
  279.              end;
  280.  
  281. -- ---------------------------------------------------------------------------------------------
  282. --
  283. -- delete_user - deletes entries that are related to a user
  284. --
  285. -- It deletes the related entries in the 'matrix' and 'user_params'.
  286. --
  287. -- ---------------------------------------------------------------------------------------------
  288.  
  289. create trigger delete_user delete on users
  290.              begin
  291.                  delete from buckets where userid = old.id;
  292.                  delete from user_params where userid = old.id;
  293.              end;
  294.  
  295. -- ---------------------------------------------------------------------------------------------
  296. --
  297. -- delete_magnet_type - handles the removal of a magnet type (this should be a
  298. --                      very rare thing)
  299. --
  300. -- ---------------------------------------------------------------------------------------------
  301.  
  302. create trigger delete_magnet_type delete on magnet_types
  303.              begin
  304.                  delete from magnets where mtid = old.id;
  305.              end;
  306.  
  307. -- ---------------------------------------------------------------------------------------------
  308. --
  309. -- delete_user_template - handles the removal of a type of user parameters
  310. --
  311. -- ---------------------------------------------------------------------------------------------
  312.  
  313. create trigger delete_user_template delete on user_template
  314.              begin
  315.                  delete from user_params where utid = old.id;
  316.              end;
  317.  
  318. -- ---------------------------------------------------------------------------------------------
  319. --
  320. -- delete_bucket_template - handles the removal of a type of bucket parameters
  321. --
  322. -- ---------------------------------------------------------------------------------------------
  323.  
  324. create trigger delete_bucket_template delete on bucket_template
  325.              begin
  326.                  delete from bucket_params where btid = old.id;
  327.              end;
  328.  
  329. -- Default data
  330.  
  331. -- There's always a user called 'admin'
  332.  
  333. insert into users ( name, password ) values ( 'admin', 'e11f180f4a31d8caface8e62994abfaf' );
  334.  
  335. -- These are the possible parameters for a bucket
  336. --
  337. -- subject      1 if should do subject modification for message classified to this bucket
  338. -- xtc          1 if should add X-Text-Classification header
  339. -- xpl          1 if should add X-POPFile-Link header
  340. -- fncount      Number of messages that were incorrectly classified, and meant to go into
  341. --                  this bucket but did not
  342. -- fpcount      Number of messages that were incorrectly classified into this bucket
  343. -- quarantine   1 if should quaratine (i.e. RFC822 wrap) messages in this bucket
  344. -- count        Total number of messages classified into this bucket
  345. -- color        The color used for this bucket in the UI
  346.  
  347. insert into bucket_template ( name, def ) values ( 'subject',    '1' ); 
  348. insert into bucket_template ( name, def ) values ( 'xtc',        '1' );
  349. insert into bucket_template ( name, def ) values ( 'xpl',        '1' );
  350. insert into bucket_template ( name, def ) values ( 'fncount',    '0' );
  351. insert into bucket_template ( name, def ) values ( 'fpcount',    '0' );
  352. insert into bucket_template ( name, def ) values ( 'quarantine', '0' );
  353. insert into bucket_template ( name, def ) values ( 'count',      '0' );
  354. insert into bucket_template ( name, def ) values ( 'color',      'black' );
  355.  
  356. -- The possible magnet types
  357.  
  358. insert into magnet_types ( mtype, header ) values ( 'from',    'From'    );
  359. insert into magnet_types ( mtype, header ) values ( 'to',      'To'      );
  360. insert into magnet_types ( mtype, header ) values ( 'subject', 'Subject' );
  361. insert into magnet_types ( mtype, header ) values ( 'cc',      'Cc'      );
  362.  
  363. -- There's always a bucket called 'unclassified' which is where POPFile puts
  364. -- messages that it isn't sure about.
  365.  
  366. insert into buckets ( name, pseudo, userid ) values ( 'unclassified', 1, 1 );
  367.  
  368. -- END
  369.  
  370.